<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Out extends MY_Controller
{
	private $datas;
	private $coins;

	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
		$this->coins = $this->d->rows(array('table' => 'coin', 'where' => 'status=1'));
	}

	public function index()
	{
		$coin_id = intval($this->input->get('cid'));

		if ($coin_id) {
			$coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $coin_id . ' and status=1'));
		}
		else {
			$coin = $this->d->find(array('table' => 'coin', 'where' => 'status=1', 'order' => 'sort asc'));
		}

		if (!$coin) {
			error404();
		}

		$d['xnb'] = $this->db->where('user_id', $this->user['id'])->where('coin_id', $coin['id'])->get('xnb')->row_array();
		$d['address'] = $this->db->where('user_id', $this->user['id'])->where('coin_id', $coin['id'])->get('address')->result_array();
		$d['status'] = intval(isset($_COOKIE['select_status']) ? $_COOKIE['select_status'] : -1);

		if (in_array($d['status'], array(0, 1, 2, 3, 4))) {
			$where['status'] = $d['status'];
		}

		$where['user_id'] = $this->user['id'];
		$result = $this->result_list(array('table' => 'draw_coin', 'where' => $where, 'url' => 'out/index', 'order' => 'add_time desc', 'per_page' => 10));
		$d['page'] = $result['page'];
		$list = $result['list'];

		foreach ($list as $k => $v) {
			$list[$k]['address'] = $this->db->where('id', $v['address_id'])->get('address')->row_array();
		}

		$d['list'] = $list;
		$d['coins'] = $this->coins;
		$d['coin'] = $coin;
		$this->show(array('header', 'user_left', 'coin_draw', 'footer'), $d);
	}

	public function insert()
	{
		$this->verify_ajax();
		$paypwd = $this->input->post('paypwd');
		if (!$paypwd || !$this->pwd->verify($paypwd, $this->user['paypwd'])) {
			ajax_return(1, '安全密码错误');
		}

		$mobile_captcha = $this->input->post('mobile_captcha');
		if (!$mobile_captcha || ($mobile_captcha != $this->session->old_mobile_captcha)) {
			ajax_return(1, '短信验证码错误');
		}

		$coin_id = intval($this->input->post('coin_id'));

		if (!$coin_id) {
			ajax_return(1, '找不到币种');
		}

		$coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $coin_id));

		if (!$coin) {
			ajax_return(1, '找不到币种');
		}

		$address_id = intval($this->input->post('address_id'));

		if (!$address_id) {
			ajax_return(1, '找不到提现地址');
		}

		$address = $this->db->where('id', $address_id)->where('user_id', $this->user['id'])->get('address')->row_array();

		if (!$address) {
			ajax_return(1, '找不到提现地址');
		}

		$num = num($this->input->post('num'), 4);

		if (!$num) {
			ajax_return(1, '提现数量不能为空');
		}

		if ($num < $this->cfg['xnb_draw_min']) {
			ajax_return(1, '提现数量不能低于' . $this->cfg['xnb_draw_min']);
		}

		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_xnb write, t_draw_coin write');
		$xnb = $this->db->where('user_id', $this->user['id'])->where('coin_id', $coin_id)->get('xnb')->row_array();

		if ($xnb['num'] < $num) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '余额不足');
		}

		$rs = array();
		$rs[] = $this->db->where('id', $xnb['id'])->set('num', 'num-' . $num, false)->update('xnb');
		$fee = num(($num * $this->cfg['xnb_draw_fee']) / 100);
		$real_num = num($num - $fee);
		$data['coin_id'] = $coin_id;
		$data['address_id'] = $address_id;
		$data['num'] = $num;
		$data['fee'] = $fee;
		$data['real_num'] = $real_num;
		$data['balance'] = $xnb['num'];
		$data['add_time'] = time();
		$data['user_id'] = $this->user['id'];
		$rs[] = $this->db->insert('draw_coin', $data);

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '提现订单已提交，请等待审核', site_url('out'));
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '提现数据验证失败，请重新填写');
		}
	}

	public function cancel()
	{
		$id = intval($this->input->post('id'));

		if ($id <= 0) {
			ajax_return(1, '找不到订单');
		}

		$draw = $this->db->where('id', $id)->where('status', 0)->where('user_id', $this->session->user_id)->get('draw_coin')->row_array();

		if (!$draw) {
			ajax_return(1, '找不到可撤销的订单');
		}

		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_xnb write, t_draw_coin write');
		$rs[] = $this->db->where('user_id', $this->user['id'])->where('coin_id', $draw['coin_id'])->set('num', 'num+' . $draw['num'], false)->update('xnb');
		$rs[] = $this->db->where('id', $id)->set('status', 3)->update('draw_coin');

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '撤销成功');
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '撤销失败');
		}
	}
}

?>
